#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : python_test10_12.py
# Author    : myh

# 练习 10.12：记住喜欢的数　将你在完成练习 10.11 时编写的两个程序
# 合而为一。如果存储了用户喜欢的数，就向用户显示它，否则提示用
# 户输入自己喜欢的数并将其存储在文件中。运行这个程序两次，看看
# 它是否像预期的那样工作。
from pathlib import Path
import json


def f_input_number():
    f_username = input("What is your favorite number? ")
    f_contents = json.dumps(int(f_username))
    path.write_text(f_contents)


path = Path('favorite number.json')
try :
    "检查文件名"
    favorite_number  = path.read_text()
except FileNotFoundError:
    f_input_number()
else:
    "判断是否为空"
    if len(favorite_number) == 0:
        # favorite_number = json.loads(favorite_number)
        f_input_number()

print(f"I know your favorite number! It's {path.read_text()}" )

